<!DOCTYPE html>
<html class="x-admin-sm">
<html lang="en" xmlns:th="http://wwww.thymeleaf.org" >
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="../aaa/css/font.css">
        <link rel="stylesheet" href="../aaa/css/xadmin.css">
        <script type="text/javascript" src="../aaa/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../aaa/js/xadmin.js"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
<!--    th:action="@{/commodity/commadd}" method="post"-->
        <div class="layui-fluid">
            <div class="layui-row" >
                <form class="layui-form"  enctype="multipart/form-data" id="fm">
                  <div class="layui-form-item">


                      <label for="commodityname" class="layui-form-label">
                          <span class="x-red">*</span>商品名
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="commodityname" name="commodityname" required="" lay-verify="required"
                          autocomplete="off" class="layui-input" >
                      </div>

                  </div>
                    <div class="layui-form-item">

                        <label for="commodityname" class="layui-form-label">
                            <span class="x-red">*</span> 审核状态
                        </label>
                        <input type="radio" name="statusid" value="2" title="已审核">
                        <input type="radio" name="statusid" value="1" title="未审核">
                    </div>

                    <div class="layui-form-item">

                        <label for="img" class="layui-form-label">
                            <span class="x-red">*</span>商品图片
                        </label>
                        <div class="layui-input-inline">
                           <input type="file" id="img" name="img">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">供应商</label>
                        <div class="layui-input-block" style="width: 180px">
                            <select  name="supId" class="form-control">
                                <option value="0">请选择</option>
                                <option th:each="supplier:${supplier}"
                                        th:value="${supplier.supId}"
                                        th:text="${supplier.supName}"></option>
                            </select>
                        </div>
                    </div>
                  <div class="layui-form-item">
                      <label for="coprice" class="layui-form-label">
                          <span class="x-red">*</span>商品单价
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="coprice" name="coprice" required=""
                          autocomplete="off" class="layui-input" lay-verify="required">
                      </div>
                  </div>
                  <div class="layui-form-item" >


                      <button type="submit" class="layui-btn" lay-filter="add" lay-submit="">提交</button>
                  </div>
              </form>
            </div>
        </div>
        <script>
            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;


//                     $(function() {
// //关闭弹窗
//                         $(document).on('click', '.layui-btn',
//                             function() {
//                                 var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
//                                 parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
//                                 parent.layer.close(index); //再执行关闭
//
//                             });
//                     });

                    // 监听提交
                    form.on('submit(add)',
                        function(data) {
                            // console.log(data.field);
                            // var datas=data.field;
                            // if (datas.status=="on"){
                            //     datas.status=0;//正常状态
                            // }else {
                            //     datas.status=1;
                            // }
                            var formData = new FormData($( "#fm" )[0]);
                            console.info(formData);
                            var saveLoading = parent.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                            $.ajax({
                                url: "/commodity/commadd",
                                type: "POST",

                                data:formData,
                                cache:false,
                                contentType: false,
                                processData: false,

                                success: function (result) {

                                    if (result.code==200){
                                        layer.msg("添加失败");
                                    }
                                    else {
                                        layer.msg("添加成功",{
                                            icon: 6,
                                            time:500
                                        },function() {
                                            parent.window.location.reload();//重新页面
                                            // 获得frame索引
                                            var index = parent.layer.getFrameIndex(window.name);
                                            //关闭当前frame
                                            parent.layer.close(index);
                                        });
                                    }
                                }, error:function (e) {
                                    //返回500错误 或者其他 http状态码错误时 需要在error 回调函数中处理了 并且返回的数据还不能直接alert，需要使用
                                    //$.parseJSON 进行转译    res.msg 是自己组装的错误信息通用变量
                                    var res = $.parseJSON(e.responseText);
                                    layer.msg("添加失败,检查数据填输是否完整");
                                }
                            });
                            return false;
                        });


                });
        </script>

    </body>

</html>
